package com.wolfpire.resource.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.resource.controller.service.ResourceService;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.model.Attachment;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.LessonPlan;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.AttachmentService;

/**
 * @author xp
 *
 */

@Controller
@RequestMapping("/resource")
public class ResourceController {
	
	@Resource private ResourceService resourceService;
	@Resource private SubjectService subjectService;
	@Resource AttachmentService attachmentService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		ModelAndView mv =  new ModelAndView("resourceManage/resource_list", "operAuthorities", operAuthorities);
		//List<Subject> subjects = this.subjectService.list(new Subject());       //获取学科
		//mv.getModelMap().put("subjects", subjects);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<LessonPlan> queryList(EasyuiPage<LessonPlan> page, LessonPlan filterLessonPlan) {
		Page<LessonPlan> lessonPlans = this.resourceService.setPageDataList(page, filterLessonPlan);
		return new EasyuiPagination<LessonPlan>(lessonPlans);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, @RequestParam(value = "subjectId", required = false) String subjectId,
			@RequestParam(value = "knowledgeId", required = false) String knowledgeId) {
		ModelAndView mv = new ModelAndView("lessonPlanManage/lessonplan_add");
		mv.getModelMap().put("subjectId", StringUtils.isBlank(subjectId) ? StringUtils.EMPTY : subjectId);
		mv.getModelMap().put("knowledgeId", StringUtils.isBlank(knowledgeId) ? StringUtils.EMPTY : knowledgeId);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(LessonPlan newLessonPlan, @RequestParam(value = "subjectId", required = false) String subjectId,
			@PrincipalArgument User user) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = false;
		String msg = null;
		// 判断添加的用户是否存在
		LessonPlan targetLessonPlan = this.resourceService.getByTitle(newLessonPlan.getTitle());
		// 新增
		if (null == targetLessonPlan) {
			newLessonPlan.setName(this.subjectService.get(Long.valueOf(subjectId)).getName());
			newLessonPlan.setPeople(user.getAccount());
			this.resourceService.saveOrUpdate(newLessonPlan);
			result = true;
			msg = "新增成功";
		} else {
			// 更新
			newLessonPlan.setName(this.subjectService.get(Long.valueOf(subjectId)).getName());
			newLessonPlan.setPeople(user.getAccount());
			this.resourceService.saveOrUpdate(newLessonPlan);
			result = true;
			msg = "更新成功";
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(Long id) {
		try {
			this.resourceService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("lessonPlanManage/lessonplan_edit");
		if (null != id) {
//			User user = this.userService.get(id);
			LessonPlan lessonPlan = this.resourceService.getCompleteLessonPlan(id);
			mv.getModelMap().put("lessonPlan", lessonPlan);
		}
		return mv;
	}
	 
	@ResponseBody
	@RequestMapping("/queryBySubjectId")
	public EasyuiPagination<Map<String, Object>> queryBySubjectId(
			EasyuiPage<Map<String, Object>> page,
			@RequestParam(value = "subjectId", required = false) Long subjectId,
			@RequestParam(value = "professionId", required = false) Long professionId,
			Attachment filterAttachment) {
		Page<Map<String, Object>> attachments = this.attachmentService.setAttachmentsBySubjectId(page, subjectId, professionId, filterAttachment);
		return new EasyuiPagination<Map<String, Object>>(attachments);
	}
}
